#include "chooselevelscene.h"
#include "playscene.h"
#include "mypushbutton.h"
#include <QAction>
#include <QMenuBar>
#include <QPainter>
#include <QTimer>
#include <QLabel>
#include <QDebug>
#include <QSound>
ChooselevelScene::ChooselevelScene(QWidget *parent)
    : QMainWindow{parent}
{
    QSound * backSound = new QSound(":/res/BackButtonSound.wav",this);
    QSound * chooseSound = new QSound(":/res/TapButtonSound.wav",this);

    this->setFixedSize(630,1176);
    this->setWindowTitle("关卡选择");
    this->setWindowIcon(QIcon("://res/Coin0001.png"));
    QMenuBar * bar = menuBar();
    this->setMenuBar(bar);
    QMenu * startMenu = bar->addMenu("开始a");
    QAction * actionQuit = new QAction("退出a");
    startMenu->addAction(actionQuit);
    connect(actionQuit,&QAction::triggered,[=](){
       this->close();
    });

    MyPushButton * backBtn = new MyPushButton(":/res/BackButton.png",":/res/BackButtonSelected.png",2,2);
    backBtn->setParent(this);
    backBtn->move(this->width()-backBtn->width(),this->height()-backBtn->height());

    connect(backBtn,&QPushButton::clicked,[=](){
        QTimer::singleShot(200,this,[=](){
            emit backMainscene();
        });
    });

    for(int i =0 ;i < 20;i++){
        MyPushButton * chooseBtn = new MyPushButton(":/res/LevelIcon.png","",2,2);
        chooseBtn->setParent(this);
        chooseBtn->move(30+i%4*150,500+i/4*120);
        connect(chooseBtn,&QPushButton::clicked,[=](){
            chooseSound->play();
            this->hide();
            playScene = new PlayScene(i+1);

            playScene->setGeometry(this->geometry());

            playScene->show();

            connect(playScene,&PlayScene::backChooseScene,[=](){
               backSound->play();
               this->setGeometry(playScene->geometry());
               this->show();
               delete playScene;
               playScene = NULL;
            });
        });

        QLabel * label = new QLabel(QString::number(i+1),this);
        label->setFixedSize(chooseBtn->width(),chooseBtn->height());
        label->move(30+i%4*150,500+i/4*120);
        label->setAlignment(Qt::AlignCenter);//设置居中
        label->setFont(QFont("楷体", 20, QFont::Bold));
        label->setAttribute(Qt::WA_TransparentForMouseEvents);
    }
}

void ChooselevelScene::paintEvent(QPaintEvent *){
    QPainter painter(this);
    QPixmap pixmap;
    pixmap.load(":/res/MenuSceneBg.png");
    painter.drawPixmap(0,-100,this->width(),this->height()+100,pixmap);
}
